/*
 *--------------------------------------------------------------------------
 *   Battlefield - A Realtime Network Multiplayer Game
 *   =======================================================
 *   Developed by Group D02 - 2009/2010 Semester 4 - CS2103
 *     Harry Nguyen Duy Hoang <nnduyhoang@yahoo.co.uk>
 *     Kent Chng Siang Rong <fivefootway@gmail.com>
 *     Lim Yong Peng <limpeng1986@gmail.com>
 *     Loh Xiankun <u0807185@nus.edu.sg>
 *   Instructed by
 *     Dr. Damith C.Rajapakse <damith@gmail.com>
 *   =======================================================
 *   $Id: Unit.java 607 2010-07-27 07:16:34Z Harry $
 *   $LastChangedDate: 2010-07-27 07:16:34 +0000 (Tue, 27 Jul 2010) $
 *   $LastChangedBy: Harry $
 *--------------------------------------------------------------------------
 */
package battlefield.entity;

import battlefield.state.CellState;
import battlefield.state.UnitType;
import java.io.Serializable;

/**
 *
 * @author Harry Nguyen
 */
public class Unit extends Grid implements Serializable {

    private int id;
    private String name;
    private int x;
    private int y;
    private int price;
    private UnitModel model;

    /**
     * Constructs the Unit object with the following parameters
     * @param id
     * @param model
     */
    public Unit(int id, UnitModel model) {
        super(model.getRows(), model.getColumns());
        this.id = id;
        this.model = model;
        this.name = model.getName();
        for (int i = 0; i < model.getRows(); i++) {
            for (int j = 0; j < model.getColumns(); j++) {
                if (!model.getTransparency()[i][j]) {
                    UnitCell unitCell = new UnitCell();
                    unitCell.setUnitImage(new CellImage(model.getUnitImage(), j * Cell.WIDTH, i * Cell.HEIGHT, Cell.WIDTH, Cell.HEIGHT));
                    unitCell.setDestroyedImage(new CellImage(model.getDestroyedImage(), 0, 0, Cell.WIDTH, Cell.HEIGHT));
                    unitCell.setState(CellState.NORMAL);
                    setCellAt(unitCell, i, j);
                }
            }
        }
    }

    /**
     * Returns the id of the unit
     * @return int
     */
    public int getId() {
        return id;
    }

    /**
     * Sets the id of the unit
     * @param id
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * Returns the name of the player
     * @return String
     */
    public String getName() {
        return name;
    }

    /**
     * Sets the name of the player
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Returns the x position of the unit
     * @return int
     */
    public int getX() {
        return x;
    }

    /**
     * Sets the x position of the unit
     * @param x
     */
    public void setX(int x) {
        this.x = x;
    }

    /**
     * Returns the y position of the unit
     * @return int
     */
    public int getY() {
        return y;
    }

    /**
     * Sets the y position of the unit
     * @param y
     */
    public void setY(int y) {
        this.y = y;
    }

    /**
     * Returns the price of the unit
     * @return int
     */
    public int getPrice() {
        return price;
    }

    /**
     * Sets the price of the unit
     * @param price
     */
    public void setPrice(int price) {
        this.price = price;
    }

    /**
     * Returns the UnitModel object of the unit
     * @return UnitModel
     * @see UnitModel
     */
    public UnitModel getModel() {
        return model;
    }

    /**
     * Sets the UnitModel of the unit object
     * @param model
     */
    public void setModel(UnitModel model) {
        this.model = model;
    }

    /**
     * Returns the UnitType of the unit
     * @return UnitType
     * @see UnitType
     */
    public UnitType getType() {
        return model.getType();
    }

    /**
     * Returns whether the unit is destroyed or not
     * @return boolean
     */
    public boolean isDestroyed() {
        for (int i = 0; i < cellGrid.length; i++) {
            for (int j = 0; j < cellGrid[i].length; j++) {
                if (cellGrid[i][j] != null && cellGrid[i][j].getState() == CellState.NORMAL) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * Returns if the unit if damage or not
     * @return boolean
     */
    public boolean isDamaged() {
        for (int i = 0; i < cellGrid.length; i++) {
            for (int j = 0; j < cellGrid[i].length; j++) {
                if (cellGrid[i][j] != null && cellGrid[i][j].getState() == CellState.DESTROYED) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }

        if (!(obj instanceof Unit)) {
            return false;
        }

        return ((Unit) obj).getId() == this.getId();
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 37 * hash + this.id;
        return hash;
    }
}
